$(function () {
    var _thisPage = null, user = null;

    var editnickPage = function () {
        this.init()
    }

    editnickPage.prototype = {
        init: function () {
            this.initData();
            this.bindEvent();
        },
        initData: function () {
            user = getLocalStorage(STORAGE_MANAGER.user);
            if (!user) {
                redirect('login.html');
            }
        },
        bindEvent: function () {

            //点击回退
            $("#back").on("tap", function () {
                back();
            });

            //点击清除
            $("#clear").on("tap", function () {
                $("#nickname").val("");
            });

            //点击保存
            $("#saveNick").on("tap", function () {
                if ("" == $("#nickname").val().trim()) {
                    $.toast('请填写昵称');
                    return false;
                } else if (!isNickname($("#nickname").val())) {
                    $.toast("昵称格式不正确");
                    return false;
                } else {
                    $.ajax({
                        type: "POST",
                        url: CONFIG_MANAGER.RequstUrl + "v1/member/nickname/" + user.id,
                        async: true,
                        data: JSON.stringify(  $("#nickname").val().trim() ),
                        contentType: "application/json; charset=UTF-8",
                        headers: {"token": user.token},
                    }).done(function (data) {
                        $.alert("修改成功", function () {
                            redirect("account.html");
                            return false;
                        });
                    }).fail(function (data) {
                        $.toast(data);
                    });
                }
            });
        }
    }

    _thisPage = new editnickPage();

})
